package com.ai.dmc.index.web.service.impl;

import com.ai.frame.bean.OutputObject;
import com.ai.frame.logger.Logger;
import com.ai.frame.logger.LoggerFactory;
import com.ai.dmc.index.web.service.ICacheService;
import com.opensymphony.oscache.base.EntryRefreshPolicy;
import com.opensymphony.oscache.base.NeedsRefreshException;
import com.opensymphony.oscache.general.GeneralCacheAdministrator;
import com.opensymphony.oscache.web.filter.ExpiresRefreshPolicy;

@SuppressWarnings("serial")
public class CacheServiceImpl extends GeneralCacheAdministrator implements
		ICacheService {
	private static final Logger logger = LoggerFactory.getServiceLog(ICacheService.class);
	
	/**
	 *  Put OutputObject to Cache
	 */
	public boolean put2Cache(String cacheKey, OutputObject outputObject) {
		return this.put2Cache(cacheKey, outputObject, -1);
	}
	
	/**
	 *  Put OutputObject to Cache
	 */
	public boolean put2Cache(String cacheKey, OutputObject outputObject,
			long expiry) {
		logger.info("put2Cache", ">------  Start ------", "cacheKey = " + cacheKey);
		this.put(cacheKey, outputObject, expiry);
		logger.info("put2Cache", "------  End ------<");
		return true;
	}

	/**
	 *  Get OutputObject from Cache
	 */
	public OutputObject getFromCache(String cacheKey) {
		logger.info("getFromCache", ">------  Start ------", "cacheKey = " + cacheKey);
		OutputObject outputObject = (OutputObject) this.get(cacheKey);
		logger.info("getFromCache", "------  End ------<", "outputObject = " + outputObject);
		return outputObject;
	}

	/**
	 * Remove All Caches
	 */
	public void removeAll() {
		logger.info("remove All Cache:", ">------  Start ------");
		this.flushAll();
		logger.info("remove All Cache:", "------  End ------<");
	}
	
	/**
	 * 保存对象到缓存中
	 * 
	 * @param key
	 *            对象在缓存系统中的Key
	 * @param o
	 *            对象实例
	 * @param expiry
	 *            缓存过期时间(s) 3600s = 1h
	 * @param group
	 *            缓存区域
	 * @return 保存是否成功
	 */
	protected boolean put(String myKey, Object myValue, long expiry,
			String group) {
		try {
			EntryRefreshPolicy policy = new ExpiresRefreshPolicy((int) expiry);
			if (group == null) {
				this.putInCache(myKey, myValue, policy);
			} else {
				this.putInCache(myKey, myValue, new String[] { group }, policy);
			}
			return true;
		} catch (Exception ex) {
			this.cancelUpdate(myKey);
		}
		return false;
	}

	/**
	 * 保存对象到缓存中
	 * 
	 * @param key
	 *            对象在缓存系统中的Key
	 * @param o
	 *            对象实例
	 * @param expiry
	 *            缓存过期时间(s) 3600s = 1h
	 * @return 保存是否成功
	 */
	protected boolean put(String key, Object o, long expiry) {
		return put(key, o, expiry, null);
	}

	/**
	 * 从缓存中删除对象
	 * 
	 * 注意！！！如果put时加了group参数，则remove时也要加group参数
	 * 
	 * @param key
	 *            对象在缓存系统中的Key
	 * @return 删除是否成功
	 */
	protected boolean remove(String key) {
		this.flushEntry(key);
		return true;
	}

	/**
	 * 从缓存中读取对象
	 * 
	 * 注意！！！如果put时加了group参数，则get时也要加group参数
	 * 
	 * @param key
	 *            对象在缓存系统中的Key
	 * @return 对象实例
	 */
	protected Object get(String key) {
		try {
			return super.getFromCache(key);
		} catch (NeedsRefreshException e) {
			this.cancelUpdate(key);
			return null;
		}
	}

	/**
	 * 从缓存中读取对象
	 * 
	 * @param key
	 *            对象在缓存系统中的Key
	 * @param group
	 *            缓存区域
	 * @return 对象实例
	 */
	protected Object get(String key, String group) {
		return get(key);
	}

	/**
	 * 从缓存中删除对象
	 * 
	 * @param key
	 *            对象在缓存系统中的Key
	 * @param group
	 *            缓存区域
	 * @return 删除是否成功
	 */
	protected boolean remove(String key, String group) {
		return remove(key);
	}
}
